package com.example.makerspace.manager;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.BookDAO;
import com.example.makerspace.domain.BookDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class BookManager extends BaseManager<BookDO, BookDTO>{
    private final static String BOOK_PICTURE_PATH_PREFIX = "book/image/";
    private final static String BOOK_RESOURCE_PATH_PREFIX = "book/resource/";

    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "book_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";
    @Resource
    private BookDAO bookDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = bookDAO;
        managerMap.put("book", this);
    }

    public Result addBook(BookDTO bookDTO) {
        BookDO oldBookDO = bookDAO.selectByBookName(bookDTO.getBookName());
        if(oldBookDO != null){
            return Result.error(ResultCodeEnum.BOOK_EXIST);
        }
        String imagePath = FileUtils.saveMultipartFile(bookDTO.getPicture(),  FileUtils.staticPath + BOOK_PICTURE_PATH_PREFIX + bookDTO.getBookName());
        String resourcePath = FileUtils.saveMultipartFile(bookDTO.getResource(),  FileUtils.staticPath + BOOK_RESOURCE_PATH_PREFIX + bookDTO.getBookName());
        BookDO bookDO = new BookDO();
        BeanUtils.copyProperties(bookDTO, bookDO);
        bookDO.setPicture(imagePath);
        bookDO.setResource(resourcePath);
        bookDAO.insert(bookDO);
        return Result.success();
    }

    public Result updateBook(BookDTO bookDTO) {
        BookDO bookDO = bookDAO.selectById(bookDTO.getId());
        if(bookDO == null){
            return Result.error(ResultCodeEnum.BOOK_NOT_EXIST);
        }
        Optional.ofNullable(bookDTO.getBookName()).ifPresent(bookDO::setBookName);
        Optional.ofNullable(bookDTO.getLevel()).ifPresent(bookDO::setLevel);
        Optional.ofNullable(bookDTO.getKeyword()).ifPresent(bookDO::setKeyword);
        Optional.ofNullable(bookDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile(bookDO.getPicture());
            String imagePath = FileUtils.saveMultipartFile(picture, FileUtils.staticPath + BOOK_PICTURE_PATH_PREFIX + bookDTO.getBookName());
            bookDO.setPicture(imagePath);
        });
        Optional.ofNullable(bookDTO.getResource()).ifPresent(resource ->{
            FileUtils.deleteFile(bookDO.getResource());
            String resourcePath = FileUtils.saveMultipartFile(resource, FileUtils.staticPath + BOOK_RESOURCE_PATH_PREFIX + bookDTO.getBookName());
            bookDO.setResource(resourcePath);
        });
        Optional.of(bookDTO.getApplicantsNum()).ifPresent(bookDO::setApplicantsNum);
        return updateDO(bookDO);
    }

    public BookDO selectByID(Integer id){
        return bookDAO.selectById(id);
    }


    protected BookDTO convert2DTO(BookDO bookDO) {
        BookDTO bookDTO = super.convert2DTO(bookDO);
        String picture = bookDO.getPicture();
        String resource = bookDO.getResource();
        String imageBriefPath = FileUtils.getBriefPath(picture);
        String resourcePath = FileUtils.getBriefPath(resource);
        String imagePath = devHttpPrefix+ imageBriefPath;
        bookDTO.setPicturePath(imagePath);
        bookDTO.setResourcePath(resourcePath);
        return bookDTO;
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, BookDO.class);
    }
}
